<template>
  <div>
    <div class="toolbar">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input placeholder="店铺名称" v-model="searchShopName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="$store.dispatch('getshopList',{searchName:searchShopName})"
          >查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="shopVisible=true">添加店铺</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="$store.state.loading"
      :data="$store.state.shop.shopList"
      style="width: 100%"
    >
      <el-table-column label="店铺图片" width="180">
        <template slot-scope="scope">
          <a :href="scope.row.shopPic | imgUrl" :title="scope.row.shopName" target="_blank">
            <img height="80px" :src="scope.row.shopPic | imgUrl" alt />
          </a>
        </template>
      </el-table-column>
      <el-table-column label="店铺添加时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createTime | date}}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺类别名称" width="120">
        <template slot-scope="scope">{{scope.row.shopTypeName}}</template>
      </el-table-column>
      <el-table-column label="店铺名称" width="120">
        <template slot-scope="scope">{{scope.row.shopName}}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="addgoodsTypeList({shopTypeId:scope.row.shopTypeId,shopId:scope.row._id})"
          >添加商品类别</el-button>

          <el-button
            type="success"
            size="mini"
            @click="addgoodsList({shopTypeId:scope.row.shopTypeId,shopId:scope.row._id})"
          >添加商品</el-button>

          <el-button size="mini" type="danger" @click="deleteList(scope.row._id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加店铺 -->
    <ShopDialog v-if="shopVisible" :shopVisible.sync="shopVisible"></ShopDialog>

    <!-- 添加商品类别 -->
    <GoodsTypeDialog
      v-if="goodsTypeVisible"
      :goodsTypeVisible.sync="goodsTypeVisible"
      :shopTypeId="shopTypeId"
      :shopId="shopId"
    ></GoodsTypeDialog>

    <!-- 添加商品 -->
    <GoodsDialog
      v-if="goodsVisible"
      :goodsVisible.sync="goodsVisible"
      :shopTypeId="shopTypeId"
      :shopId="shopId"
    ></GoodsDialog>

    <!-- 分页 -->
    <PageInfo action-name="getshopList" :params="{shopName:searchShopName}"></PageInfo>
  </div>
</template>

<script>
import ShopDialog from "@/components/dialog/ShopDialog";
import GoodsTypeDialog from "@/components/dialog/GoodsTypeDialog";
import GoodsDialog from "@/components/dialog/GoodsDialog";
export default {
  name: "shop-list",
  components: {
    ShopDialog,
    GoodsTypeDialog,
    GoodsDialog
  },
  data() {
    return {
      searchShopName: "",
      shopVisible: false,
      goodsTypeVisible: false,
      goodsVisible: false,
      shopTypeId: "",
      shopId: ""
    };
  },
  methods: {
    //点击添加商品类型,获取传递各种id,同时控制添加表单显示
    addgoodsTypeList(ids) {
      this.goodsTypeVisible = true;
      this.shopTypeId = ids.shopTypeId;
      this.shopId = ids.shopId;
    },
    //点击添加商品,获取传递各种id,同时控制添加表单显示
    addgoodsList(ids) {
      this.goodsVisible = true;
      this.shopTypeId = ids.shopTypeId;
      this.shopId = ids.shopId;
    },
    deleteList(id) {
      this.$store.dispatch("deleteshopList", id).then(res => {
        if (res.ok === 1) {
          this.$store.dispatch("getshopList", {
            shopTypeName: this.searchShopTypeName,
            pageIndex: this.$store.state.pageIndex
          });
          this.$message({
            message: res.msg,
            type: "success"
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error"
          });
        }
      });
    }
  }
};
</script>